package com.tengol.demo.design.pattern.structural.flyweight.demo1;

import java.util.HashMap;
import java.util.Map;

/**
 * FlyweightFactory
 *
 * @author dongrui
 * @date 2019/03/07
 */
public class FlyweightFactory {
    //定义一个池容器
    private static final Map<String, Flyweight> POOL = new HashMap<String, Flyweight>();
    //享元工厂
    public static Flyweight getFlyweight(String extrinsic){
        Flyweight flyweight;
        if(POOL.containsKey(extrinsic)){
            //池中有该对象
            flyweight = POOL.get(extrinsic);
            System.out.println("已有" + extrinsic + "，则从池中获取.....");
        }else{
            //根据外部状态创建享元对象
            flyweight = new ConcreteFlyweight(extrinsic);
            //放入池中
            POOL.put(extrinsic,flyweight);
            System.out.println("创建" + extrinsic + "，并放入池中.....");
        }
        return flyweight;
    }
}
